/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sys.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.modules.act.service.ActTaskService;
import com.jeeplus.modules.act.utils.ActUtils;
import com.jeeplus.modules.activity.entity.ActivityRecord;
import com.jeeplus.modules.kpi.entity.AchievementInfo;
import com.jeeplus.modules.kpi.entity.AchievementInfoScore;
import com.jeeplus.modules.kpi.service.AchievementInfoScoreService;
import com.jeeplus.modules.sys.dao.ResultDao;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.echartDto.EchartDto;
import com.jeeplus.modules.sys.echartDto.ResultDto;
import com.jeeplus.modules.sys.echartDto.ResultProjectTypeDto;
import com.jeeplus.modules.sys.echartDto.ResultVo;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Result;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;

/**
 * 成果登记Service
 * 
 * @author lw
 * @version 2017-12-30
 */
@Service
@Transactional(readOnly = true)
public class ResultService extends CrudService<ResultDao<Result>, Result> {
	@Autowired
	private ActTaskService actTaskService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private ResultDao<Result> resultDao;
	@Autowired
	private AchievementInfoScoreService achievementInfoScoreService;

	public Result get(String id) {
		return super.get(id);
	}

	public List<Result> findList(Result result) {
		return super.findList(result);
	}

	public Page<Result> findPage(Page<Result> page, Result result) {
		Page<Result> findPage = super.findPage(page, result);
		List<Result> list = findPage.getList();
		for (Result result2 : list) {
			Office office = UserUtils.getUser().getOffice();
			result2.setOffice(office);
		}
		return findPage;

	}

	@Transactional(readOnly = false)
	public void save(Result result) {
		String title = "[成果审批]";
		if (StringUtils.isNoneBlank(result.getTopicName())) {
			title += result.getTopicName();
		}
		if (StringUtils.isBlank(result.getId())) {
			result.preInsert();
			// 填表日期，设置成当前的日期
			result.setCreateDate(new Date());
			resultDao.insert(result);
			// 启动流程
			actTaskService.startProcess(ActUtils.PD_RESULT_APPLY[0], ActUtils.PD_RESULT_APPLY[1], result.getId(),
					title);
			// 更新状态 ，已经发起申请了
			result.setProcessNode("2");

			try {
				// 更改流程节点状态
				resultDao.updateProcessNode(result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// 重新编辑申请
		else {
			result.preUpdate();
			result.setProcessNode("2");
			resultDao.update(result);
			result.getAct().setComment(
					("yes".equals(result.getAct().getFlag()) ? "[重申] " : "[销毁] ") + result.getAct().getComment());

			// 完成流程任务
			Map<String, Object> vars = Maps.newHashMap();
			vars.put("pass", "yes".equals(result.getAct().getFlag()) ? "1" : "0");
			actTaskService.complete(result.getAct().getTaskId(), result.getAct().getProcInsId(),
					result.getAct().getComment(), title, vars);
			if ("no".equals(result.getAct().getFlag())) {
				result.setProcessNode("0");
				resultDao.update(result);
			}

		}
	}

	@Transactional(readOnly = false)
	public void delete(Result result) {
		super.delete(result);
	}

	public Page<Result> findPageByOfficeId(Page<Result> page, String officeId, Result entity) {
		entity.setPage(page);
		Office office = new Office();
		office.setId(officeId);
		entity.setOffice(office);
		Page<Result> findPage = page.setList(resultDao.findListByOfficeId(entity));
		List<Result> list = findPage.getList();
		for (Result result : list) {
			if (StringUtils.isNotBlank(result.getIntro())) {
				result.setIntro(org.apache.commons.lang3.StringEscapeUtils.unescapeHtml4(result.getIntro()));
			}
		}
		return findPage;
	}

	public int selectCountByOfficeId(String officeId) {
		int count = resultDao.selectCountByOfficeId(officeId);
		return count;
	}

	public EchartDto findAmount(String officeId) {
		EchartDto echart = new EchartDto();
		List<ResultDto> resultList = resultDao.findAmount(officeId);
		List<ResultDto> userAmount = userDao.findAmount(officeId);
		for (ResultDto user : userAmount) {
			String job = DictUtils.getDictLabel(user.getJob(), "user_job", "其他");
			user.setJob(job);
			;
		}
		echart.setResultList(resultList);
		echart.setUserList(userAmount);
		return echart;
	}

	public Integer findCountByOffice(Result result) {
		return resultDao.findCountByOffice(result);
	}

	public Integer findCountByDate(Result result) {
		return resultDao.findCountByDate(result);
	}

	public Integer findCountByUser(Result result) {
		return resultDao.findCountByUser(result);
	}

	@Transactional(readOnly = false)
	public void approveSave(Result result) {
		// 设置意见
		result.getAct().setComment(
				("yes".equals(result.getAct().getFlag()) ? "[同意] " : "[驳回] ") + result.getAct().getComment());

		result.preUpdate();

		// 对不同环节的业务逻辑进行操作
		String taskDefKey = result.getAct().getTaskDefKey();

		// 审核环节
		if ("dept-review".equals(taskDefKey)) {
			result.setDeptApproveAdvice(result.getAct().getComment());
		} else if ("dept-leader-review".equals(taskDefKey)) {
			result.setDeptLeaderAdvice(result.getAct().getComment());
		}

		else if ("sci-research-magt-review".equals(taskDefKey)) {
			result.setSciresearchDeptAdvice(result.getAct().getComment());
		} else if ("big-boss-review".equals(taskDefKey)) {
			result.setBossLeaderAdvice(result.getAct().getComment());
		}
		// 未知环节，直接返回
		else {
			return;
		}
		// 更新数据
		resultDao.update(result);

		// 提交流程任务
		Map<String, Object> vars = Maps.newHashMap();
		// 最后一步如果同意，则更新项目信息状态
		if ("big-boss-review".equals(taskDefKey)) {
			// vars.put("fin_pass", "yes".equals(activity.getAct().getFlag())? "1" : "0");
			if ("yes".equals(result.getAct().getFlag())) {

				result.setProcessNode("3");
				result.setProcessEndtime(new Date());
				try {
					resultDao.updateProcessNode(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		if (!"yes".equals(result.getAct().getFlag())) {
			result.setProcessNode("1");
			try {
				resultDao.updateProcessNode(result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		vars.put("pass", "yes".equals(result.getAct().getFlag()) ? "1" : "0");
		actTaskService.complete(result.getAct().getTaskId(), result.getAct().getProcInsId(),
				result.getAct().getComment(), vars);

	}

	public List<ResultProjectTypeDto> echartProjectType() {
		List<ResultProjectTypeDto> echartProjectType = resultDao.echartProjectType();
		for (ResultProjectTypeDto resultProjectTypeDto : echartProjectType) {
			String dictLabel = DictUtils.getDictLabel(resultProjectTypeDto.getName(), "project_type", "其他");
			resultProjectTypeDto.setName(dictLabel);
		}
		return echartProjectType;
	}
	// 科研成果发布量
	public List<ResultProjectTypeDto> selectResultRepub() {
		 List<ResultProjectTypeDto> resultRepub = resultDao.selectResultRepub();
		return resultRepub;
	}
	
}